<?php

/**
 *      [CodeJm!] Author CodeJm[codejm@163.com].
 *
 *      
 *      $Id: Wxmessage.php 2016-04-07 10:48:05 codejm $
 */

class WxmessageModel extends \Core_CMode {

    public $_table = '`wx_message`';

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => '消息id', 
            'mfromusername' => '发送者', 
            'mtousername' => '接受者', 
            'mcreatetime' => '产生时间', 
            'mtype' => '消息类型', 
            'mcontent' => '消息内容', 
            'mpicurl' => '图片链接', 
            'mmediaid' => '图片消息媒体id', 
            'mformat' => '语音格式', 
            'mthumbmediaid' => '视频消息缩略图id', 
            'mtitle' => '消息标题', 
            'mdescription' => '消息描述', 
            'murl' => '消息链接', 
            'mid' => '微信服务器消息编号', 
            'mstate' => '消息状态0为未回复',
        );
    }

    /**
     * 验证规则
     *
     */
    public function rules() {
        return array(
            'default'=> array(
                array('mfromusername', 'required|maxlength[30]'),
                array('mtousername', 'required|maxlength[30]'),
                array('mcreatetime', 'maxlength[10]'),
                array('mtype', 'maxlength[10]'),
                array('mmediaid', 'maxlength[11]|integer'),
                array('mformat', 'maxlength[20]'),
                array('mthumbmediaid', 'maxlength[11]|integer'),
                array('mtitle', 'maxlength[200]'),
                array('mdescription', 'maxlength[250]'),
                array('mid', 'maxlength[11]|integer'),
                array('mstate', 'maxlength[4]|integer'),

            ),
        );
    }

    /**
     * 批量删除
     *
     */
    public function delWxmessages($ids) {
        $ids_str = implode(',', array_fill(0, count($ids), '?'));
        $where = 'id IN ('.$ids_str.')';
        return $this->update($where, array('status'=>-1), $ids);
    }

    // 服务器插入数据
    public function recordUserMessage ($messageinfos) {
        $colnames = '';
        $messagevaule = '';
        if(!empty($messageinfos)) {
            foreach($messageinfos as $name => $v) {
                //字段名字
                $colnames .= $name . ',';
                //字段值
                $messagevaule .= $v . ',';
            }
        }
        //去除多余的符号
        $colnames = trim($colnames,',');
        $messagevaule = trim($messagevaule,',');

        //sql
        $sql = "insert into " . $this->_table . "($colnames) values($messagevaule)" ;
        //执行sql语句
        $result = $this->db->prepare($sql);
        $row = $result->execute();
        $result->closeCursor();

        return $row;
    }
}

?>
